package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.domain.HandleAlertData;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.mapper.NursingElderMapper;
import com.zzyl.nursing.service.IAlertDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 报警数据Service业务层处理
 * 
 * @author mr.wu
 * @date 2025-05-29
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private NursingElderMapper nursingElderMapper;


    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {

        // 获取当前登录人id
        Long userId = SecurityUtils.getUserId();
        alertData.setUserId(userId);
        List<AlertData> alertDataList = alertDataMapper.selectAlertDataList(alertData);
        if (CollUtil.isEmpty(alertDataList)){
            return alertDataList;
        }

        // 设备绑定位置
        for (AlertData data : alertDataList) {
            data.setAccessLocation(data.getRemark());
        }

        return alertDataList;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理设备报警数据
     *
     * @param handleAlertData
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void HandleAlertData(HandleAlertData handleAlertData) {
        AlertData alertData = alertDataMapper.selectAlertDataById(handleAlertData.getId());
        if (alertData==null){
            throw new BaseException("设备报警数据为空");
        } else {
            alertData.setProcessorId(SecurityUtils.getUserId());
            alertData.setProcessorName(SecurityUtils.getUsername());
            alertData.setProcessingResult(handleAlertData.getProcessingResult());
            alertData.setProcessingTime(handleAlertData.getProcessingTime());
            alertData.setStatus(1);
            alertDataMapper.handleAlertData(alertData);
        }


    }
}
